package mysql

import (
	"onerk4/zk3/common/global"
	"onerk4/zk3/common/model"
)

func QueryUser(mobile string) (result *model.User, err error) {
	err = global.DB.Where("mobile = ?", mobile).Limit(1).Find(&result).Error
	return
}

func CreateUser(m *model.User) error {
	return global.DB.Create(m).Error
}

func CreateCoupon(m *model.Coupon) error {
	return global.DB.Create(m).Error
}

func QueryStudyPlanInfo(id int64) (result *model.StudyPlan, err error) {
	err = global.DB.Where("id = ?", id).Limit(1).Find(&result).Error
	return
}

func AddStudyPlanShow(m *model.UserStudyPlan) error {
	return global.DB.Create(m).Error
}

func QueryUserStudyPlanInfo(id int64, uid int64) (result *model.UserStudyPlan, err error) {
	err = global.DB.Model(&model.UserStudyPlan{}).Where("id = ?", id).Where("uid = ?", uid).Limit(1).Find(&result).Error
	return
}

func DelStudyPlanShow(id int64, uId int64) error {
	return global.DB.Model(&model.UserStudyPlan{}).Where("id = ?", id).Where("uid = ?", uId).Limit(1).Delete(&model.UserStudyPlan{}).Error
}

func QueryUserCoupon(id int64) (result *model.Coupon, err error) {
	err = global.DB.Where("uid = ?", id).Limit(1).Find(&result).Error
	return
}

func CreateOrder(m *model.Order) error {
	return global.DB.Create(m).Error
}

func DelUserCoupon(id int64, coupon int64) error {
	return global.DB.Model(&model.Coupon{}).Where("uid = ?", id).Where("id = ?", coupon).Limit(1).Delete(&model.Coupon{}).Error
}

func UpdateOrderStatus(sn string, i int) error {
	return global.DB.Model(&model.Order{}).Where("order_num = ?", sn).Limit(1).Update("order_status", i).Error
}
